package code;

import EXtension.FrameWork.EventSystem;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.io.File;
import java.io.IOException;

public class MyThumbnail extends BorderPane{
    public final double HEIGHT_OF_THUMBNAIL=160;
    public final double WEIGHT_OF_THUMBNAIL=160;
    public final double MAX_HEIGHT_OF_THUMBNAIL=180;
    public final double MAX_WEIGHT_OF_THUMBNAIL=180;

    public BooleanProperty isSelected=new SimpleBooleanProperty(false);

    public File selectedFile;


    public MyThumbnail(File imageFile) throws IOException {

        this.getStylesheets().add(getClass().getResource("../Default.css").toExternalForm());
        selectedFile=imageFile;

        Canvas canvas=new Canvas(WEIGHT_OF_THUMBNAIL,HEIGHT_OF_THUMBNAIL);
        GraphicsContext gc=canvas.getGraphicsContext2D();
        double x,y;


       Image image=new Image(imageFile.toURI().toString(),WEIGHT_OF_THUMBNAIL,HEIGHT_OF_THUMBNAIL,true,false);
       x=(WEIGHT_OF_THUMBNAIL-image.getWidth())/2;
       y=(HEIGHT_OF_THUMBNAIL-image.getHeight())/2;

       gc.drawImage(image,x,y);
       Label label=new Label(imageFile.getName());
       label.setAlignment(Pos.BOTTOM_CENTER);
       label.prefWidthProperty().bind(canvas.widthProperty());

       this.setCenter(canvas);
       this.setBottom(label);
       this.setMaxSize(MAX_WEIGHT_OF_THUMBNAIL,MAX_HEIGHT_OF_THUMBNAIL);


       isSelected.addListener(new ChangeListener<Boolean>() {
           @Override
           public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
               //System.out.println(imageFile+"isSelected"+t1);
               MyThumbnail.this.getStyleClass().remove("thumbnail-hover");
               if(t1){
                   MyThumbnail.this.getStyleClass().add("thumbnail-hover");
               }

           }
       });
       this.setOnMouseEntered(new EventHandler<MouseEvent>() {
           @Override
           public void handle(MouseEvent mouseEvent) {
               if(!isSelected.getValue()) MyThumbnail.this.getStyleClass().add("thumbnail-hover");
           }
       });
       this.setOnMouseExited(new EventHandler<MouseEvent>() {
           @Override
           public void handle(MouseEvent mouseEvent) {

               if(!isSelected.getValue()) MyThumbnail.this.getStyleClass().remove("thumbnail-hover");
           }
       });

        this.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {

                mouseEvent.consume();
                if(mouseEvent.getButton()== MouseButton.PRIMARY){
                    if(mouseEvent.getClickCount()==2){
                        try {
                            Stage stage=new Stage();
                            BorderPane root = FXMLLoader.load(getClass().getResource("../ImageBrowser.fxml"));
                            Scene scene = new Scene(root);
                            root.prefWidthProperty().bind(scene.widthProperty());
                            root.prefHeightProperty().bind(scene.heightProperty());
                            stage.setTitle("图片查看器");
                            stage.setScene(scene);
                            stage.show();
                            EventSystem.instance.SendEvent("clickedImage",imageFile);
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    if(mouseEvent.getClickCount()==1){
                        if(mouseEvent.isControlDown()){
                            EventSystem.instance.SendEvent("AddFile",MyThumbnail.this);
                        }
                        else {
                            EventSystem.instance.SendEvent("SingleChoose",MyThumbnail.this);
                        }

                    }


                }
            }
        });
    }


}
